﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ticTacToe.Models;

namespace ticTacToe.Domain
{
    public class TttGame
    {
        public Player playerX {get; set;}                       // at least one human player
        public Player playerO { get; set; }                     // at least one human player
        public ComputerPlayer bot { get; set; }                 // the bot player
        public String gameId { get; set; }                      //  might be need to have a semi unique game id for Application wide purposes/maintaining session information
        public Gameboard gameboard { get; set; }                // the class that keeps track of the board and it's state

        public TttGame()
        {
            // TODO: Complete member initialization
        }
        //ctor to play a game with one human player and a bot
        public TttGame(UserModel user, char mark, int diff) {
            this.gameId = DateTime.Now.ToString().GetHashCode().ToString("x");
            this.gameboard = new Gameboard();
            UserModel comp = new UserModel("Computer");
            if(mark == 'X'){
                playerX = new Player(user, mark);
                playerO = new Player(comp, 'O');
                bot = new ComputerPlayer('O', diff);
            }
            else{
                playerO = new Player(user, mark);
                playerX = new Player(comp, 'X');
                bot = new ComputerPlayer('X', diff);
                //the bot is x so he has to start
                computerPlay();
            }
            
        }
        
        //ctor to play a game with two humanplayers
        public TttGame(UserModel u1, char mark, UserModel u2) {
            this.gameId = DateTime.Now.ToString().GetHashCode().ToString("x");
            this.gameboard = new Gameboard();
            if(mark == 'X'){
                playerX = new Player(u1, mark);
                playerO = new Player(u2, 'O');
               
            }
            else{
                playerO = new Player(u1, mark);
                playerX = new Player(u2, 'X');
            }
            bot = null;
            
        }

        

        public String getGameId() {
            return this.gameId;
        }

        //might need this one to throw an error if something goes wrong
        public string play(int square) {
            if(bot == null){
                //we have two players so who's turn is it
                if(gameboard.getNextPlayBelongsTo() == 'X')
                    gameboard.setMark('X', square);
                else
                    gameboard.setMark('O', square);
            }
            else{
                //we have one player against the computer so we check which mark the player has and play his move
                //the the computer plays
                if(playerX.username != "Computer"){
                    //player as X and bot as O
                    gameboard.setMark(playerX.getMark(), square);
                }
                else{
                    //bot as X against player as O
                    gameboard.setMark(playerO.getMark(), square);
                }
                validateBoard();
                if(this.gameboard.getPlayCount() < 9 && !this.gameboard.isGameOver())
                    computerPlay();
            }
            //return board state after this move
            return validateBoard(); 
        }
        
        //private func to computerplay on the board
        private void computerPlay() {
            if (!this.gameboard.isGameOver())
            {
                int compPlay = bot.play(this.gameboard);
                gameboard.setMark(bot.getMark(), compPlay);
            }
        }
        
        //function that interpets the board and returns a string describing it´s state
        //checks the board state by running GameBoard.isGameOver() function and decides what to pass on
        public string validateBoard()
        {
            String status = this.gameboard.checkForWinner();
            if (status != "" && status != null)
            {
                if (status == "X" || status == "O"){
                    String temp = new String(this.gameboard.getBoard());
                    return temp;
                }
                else if (status == "draw")
                    return "Game was a draw";
                else
                {
                    string[] split = status.Split(';');
                    string r = "";
                    if (split[2] == "XXX")
                        r += this.playerX.getUsername();
                    else
                        r += this.playerO.getUsername();
                    r += ";" + split[2] + ";" + split[1] + split[0];
                    return r;
                }
            }
            else
                return "Error: innvalid game board";
         }
            


 
    }   
}